package com.hengtiansoft.lfy.base;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.hengtiansoft.lfy.R;
import com.hengtiansoft.lfy.chart.applib.controller.HXSDKHelper;
import com.hengtiansoft.lfy.utils.ActivityManager;
import com.hengtiansoft.lfy.utils.Config;
import com.hengtiansoft.lfy.utils.Kdxf_TTSPlayer;
import com.hengtiansoft.lfy.utils.StatusBarUtil;
import com.hengtiansoft.lfy.widget.SwipeBackLayout;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;
import java.util.List;

public abstract class BaseActivity extends AppCompatActivity {
	public ActivityManager activityManager;
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	protected SwipeBackLayout mSwipeBackLayout;
	protected BaseActivity self;
	protected int width, height;
	private int beColor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mSwipeBackLayout = new SwipeBackLayout(this);
		mSwipeBackLayout.attachToActivity(this);
		mSwipeBackLayout.setBgTransparent();
		setStatusBar(R.color.white);
		self = this;
		activityManager = ActivityManager.getScreenManager();
		activityManager.pushActivity(this);
		
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		width = dm.widthPixels;
		height = dm.heightPixels;
		Config.setScreenSize(this);
	}

	/**
	 * 设置状态栏
	 * @param color 白色/灰色/其他颜色
     */
	public void setStatusBar(int color){
		if(beColor != color){
			setNormalBar(color);
			beColor = color;
		}
	}

	private void setNormalBar(int color){
		if(StatusBarUtil.StatusBarLightMode(this) == 0){
			setTranslucentStatus(true);
			if(color == R.color.white){
				StatusBarUtil.setStatusBarColor(this,R.drawable.tintbar_color);
				beColor = R.drawable.tintbar_color;
				return;
			}else{
				StatusBarUtil.setStatusBarColor(this,color);
			}
		}else{
			StatusBarUtil.setStatusBarBGColor(this,color);
		}
	}

	/**
	 * 页面重新回到台前刷新
	 */
	private void refreshBar(){
		setNormalBar(beColor);
	}

	/**
	 * 是否禁用滑动返回
	 */
	public void setSwipeBackEnabled(boolean isSwipeBackEnabled) {
		mSwipeBackLayout.setSwipeBackEnabled(isSwipeBackEnabled);
	}

	@TargetApi(19)
	private void setTranslucentStatus(boolean on) {
		Window win = getWindow();
		WindowManager.LayoutParams winParams = win.getAttributes();
		final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
		if (on) {
			winParams.flags |= bits;
		} else {
			winParams.flags &= ~bits;
		}
		win.setAttributes(winParams);
	}

	/**
	 * 递归调用，对所有子Fragement生效
	 *
	 * @param frag
	 * @param requestCode
	 * @param resultCode
	 * @param data
	 */
	private void handleResult(Fragment frag, int requestCode, int resultCode,
							  Intent data) {
		frag.onActivityResult(requestCode & 0xffff, resultCode, data);
		List<Fragment> frags = frag.getChildFragmentManager().getFragments();
		if (frags != null) {
			for (Fragment f : frags) {
				if (f != null)
					handleResult(f, requestCode, resultCode, data);
			}
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
//		JPushInterface.onResume(this);
		MobclickAgent.onResume(this);
		refreshBar();
		// onresume时，取消notification显示 ?此处锁屏后打开com.hengtiansoft.lfy.activity.MainActivity会消除通知 导致用户看不到通知
		HXSDKHelper.getInstance().getNotifier().reset();
	}

	@Override
	protected void onPause() {
		super.onPause();
//		JPushInterface.onPause(this);
		MobclickAgent.onPause(this);
		Kdxf_TTSPlayer.getInstense().stop();
	}

	@Override
	protected void onDestroy() {
		imageLoader.clearMemoryCache();
		activityManager.popActivity(this);
		super.onDestroy();
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	/**
	 * 返回
	 *
	 * @param view
	 */
	public void back(View view) {
		finish();
	}
}
